package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import top.tangyh.basic.exception.BizException;
import top.tangyh.lamp.care.entity.BCareRecordsConfig;
import top.tangyh.lamp.care.entity.BGenItemDetail;
import top.tangyh.lamp.care.mapper.BCareRecordsConfigMapper;
import top.tangyh.lamp.care.mapper.BGenItemDetailMapper;
import top.tangyh.lamp.care.mapper.BGenItemMapper;
import top.tangyh.lamp.care.service.BGenItemDetailService;
import top.tangyh.lamp.care.service.BGenItemService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BGenItemManager;
import top.tangyh.lamp.care.entity.BGenItem;
import top.tangyh.lamp.care.vo.query.GenItemQueryParam;
import top.tangyh.lamp.care.vo.result.GenItemDto;
import top.tangyh.lamp.care.vo.result.PageOutputDTO;
import top.tangyh.lamp.care.vo.save.*;
import top.tangyh.lamp.care.vo.update.BGenItemUpdateVO;
import top.tangyh.lamp.care.vo.result.BGenItemResultVO;
import top.tangyh.lamp.care.vo.query.BGenItemPageQuery;
import top.tangyh.lamp.common.constant.CommonConstant;
import top.tangyh.lamp.common.utils.ConvertUtil;
import top.tangyh.lamp.common.utils.EasyExcelUtil;
import top.tangyh.lamp.system.mapper.system.DefDictMapper;
import top.tangyh.lamp.system.vo.result.system.DefDictItemResultVO;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 通用项目配置表
 * </p>
 *
 * @author zqb
 * @date 2024-07-17 17:16:35
 * @create [2024-07-17 17:16:35] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BGenItemServiceImpl extends SuperServiceImpl<BGenItemManager, Long, BGenItem> implements BGenItemService {

    private final BGenItemMapper bGenItemMapper;

    private final BGenItemDetailService bGenItemDetailService;

    private final BGenItemDetailMapper bGenItemDetailMapper;


    private final DefDictMapper defDictMapper;

    private final BCareRecordsConfigMapper bCareRecordsConfigMapper;

    private static final String ITEM_NAME = "itemName";

    /**
     * 项目编码字段名
     */
    private static final String ITEM_CODE = "itemCode";

    /**
     * 导出XLS时默认工作薄名
     */
    private static final String SHEET_1 = "sheet1";

    @Override
    public PageOutputDTO<GenItemDto> queryPage(GenItemQueryParam param) {
        LambdaQueryWrapper<BGenItem> queryWrapper = newQueryWrapper(param);

        Integer limitStart = param.getPage() == null || param.getSize() == null ? null : (param.getPage() - 1) * param.getSize();
        List<GenItemDto> list = bGenItemMapper.findGenItemList(limitStart, param.getSize(), queryWrapper);

        PageOutputDTO<GenItemDto> rst = new PageOutputDTO<>();
        rst.setList(list);
        rst.setCurrent(Convert.toLong(param.getPage()));
        rst.setPage(Convert.toLong(param.getSize()));
        long total = bGenItemMapper.findGenItemListCount(queryWrapper);
        rst.setTotal(total);
        return rst;
    }

    private LambdaQueryWrapper<BGenItem> newQueryWrapper(GenItemQueryParam param) {
        LambdaQueryWrapper<BGenItem> queryWrapper = new LambdaQueryWrapper<>();

        if (param == null) {
            return queryWrapper;
        }
        String modelCodes = param.getModelCode();
        if (StrUtil.isNotBlank(modelCodes)) {
            param.setModelCodeList(Arrays.asList(modelCodes.split(",").clone()));
        }
        queryWrapper.eq(param.getId() != null, BGenItem::getId, param.getId())
                .eq(StrUtil.isNotBlank(param.getItemCode()), BGenItem::getItemCode, param.getItemCode())
                .in(CollectionUtil.isNotEmpty(param.getModelCodeList()), BGenItem::getModelCode, param.getModelCodeList())
                .like(StrUtil.isNotBlank(param.getItemName()), BGenItem::getItemName, param.getItemName())
                .eq(param.getIsDeleted() != null, BGenItem::getIsDeleted, param.getIsDeleted())
                .eq(param.getIsSys() != null, BGenItem::getIsSys, param.getIsSys())
                .eq(StrUtil.isNotBlank(param.getPatientIdent()), BGenItem::getPatientIdent, param.getPatientIdent());

        return queryWrapper;
    }


    @Override
    public List<BGenItem> queryAll(GenItemQueryParam param) {
        LambdaQueryWrapper<BGenItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(param.getModelCode()), BGenItem::getModelCode, param.getModelCode())
                .eq(param.getIsVisible() != null, BGenItem::getIsVisible, param.getIsVisible())
                .eq(param.getId() != null, BGenItem::getId, param.getId())
                .eq(StrUtil.isNotBlank(param.getItemCode()), BGenItem::getItemCode, param.getItemCode())
                .like(StrUtil.isNotBlank(param.getItemName()), BGenItem::getItemName, param.getItemName())
                .eq(param.getIsDeleted() != null, BGenItem::getIsDeleted, param.getIsDeleted())
                .eq(StrUtil.isNotBlank(param.getPatientIdent()), BGenItem::getPatientIdent, param.getPatientIdent())
                .eq(param.getIsSys() != null, BGenItem::getIsSys, param.getIsSys()).orderByDesc(BGenItem::getOrderNo);
//				.orderByDesc(BGenItem::getIsManual);
        List<BGenItem> rst = bGenItemMapper.selectList(queryWrapper);
        return rst;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GenItemDto insertOrUpdate(GenItemSaveParam param) {

        if (param.getIsSys() == null) {
            throw new BizException("全局性参数不能为空");
        }
        if (param.getIsSys().intValue() == 0 && StrUtil.isBlank(param.getPatientIdent())) {
            throw new BizException("病人识别号不能为空。");
        }

        GenItemDto dto = BeanUtil.copyProperties(param, GenItemDto.class);

        //主表处理
        Long id = param.getId();
        BGenItem bGenItem = BeanUtil.copyProperties(param, BGenItem.class);

        boolean isAdd = (id == null);
        BGenItem bGenItemDb = null;
        if (param.getIsSys().intValue() == 0) {
            //个性化配置，按业务主键判断是否新增
            LambdaQueryWrapper<BGenItem> headQueryWrapper = newSaveQueryWrapper(BeanUtil.copyProperties(param, GenItemDto.class));
            List<BGenItem> dbList = bGenItemMapper.selectList(headQueryWrapper);
            if (CollectionUtil.isNotEmpty(dbList)) {
                bGenItemDb = dbList.get(0);
            }
            isAdd = CollectionUtil.isEmpty(dbList);
        } else {
            //当更改了项目名称，需要同步所有个性化的配置名称
            bGenItemDb = bGenItemMapper.selectById(id);
        }
        //去重检查
        busiKeyCheck(dto, isAdd);
        if (isAdd) {
            //新增
            bGenItem.setId(null);
            bGenItemMapper.insert(bGenItem);
            id = bGenItem.getId();
            bGenItem.setId(id);
            dto.setId(id);
            param.setId(id);
        } else {
            bGenItem.setItemCode(null);
            bGenItemMapper.updateById(bGenItem);
            if (bGenItemDb != null) {
                String itemNameDb = bGenItemDb.getItemName();
                //当更改了项目名称，需要同步所有个性化的配置名称
                syncSpecialCfg(param, bGenItem, itemNameDb);
            }
        }

        //明细处理
        if (dto.getDetail() == null || dto.getDetail().isEmpty() || param.getIsSys().intValue() == 0) {
            return dto;
        }
        LambdaQueryWrapper<BGenItemDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BGenItemDetail::getGenId, id);
        bGenItemDetailMapper.delete(queryWrapper);
        Set<String> detailItemNameSet = new HashSet<>();
        for (BGenItemDetail item : param.getDetail()) {
            if (detailItemNameSet.contains(item.getItemName())) {
                throw new BizException("项目明细" + item.getItemName() + "重复");
            }
            detailItemNameSet.add(item.getItemName());
            item.setGenId(id);
            bGenItemDetailMapper.insert(item);
            item.setId(item.getId());
        }
        return dto;
    }

    /**
     * 主表业务主键重复型检查
     */
    private void busiKeyCheck(GenItemDto param, boolean isAdd) {
        if (param.getIsSys().intValue() == 0) {
            return;
        }
        busiKeyCheck(param, ITEM_NAME, isAdd, "项目名称");
        if (StrUtil.isBlank(param.getItemCode()) || 0 == Convert.toInt(param.getIsSys())) {
            return;
        }
        busiKeyCheck(param, ITEM_CODE, isAdd, "项目编码");


    }

    private LambdaQueryWrapper<BGenItem> newSaveQueryWrapper(GenItemDto param) {
        LambdaQueryWrapper<BGenItem> headQueryWrapper = new LambdaQueryWrapper<>();
        headQueryWrapper.eq(BGenItem::getModelCode, param.getModelCode());
        headQueryWrapper.eq(BGenItem::getItemName, param.getItemName());
        headQueryWrapper.eq(BGenItem::getIsDeleted, 0);
        headQueryWrapper.eq(BGenItem::getIsSys, param.getIsSys());

        if (1 == param.getIsSys().intValue()) {
            headQueryWrapper.isNull(BGenItem::getPatientIdent);
            headQueryWrapper.isNull(BGenItem::getHospitalNumber);
        } else {
            headQueryWrapper.eq(StrUtil.isNotBlank(param.getPatientIdent()), BGenItem::getPatientIdent, param.getPatientIdent());
            headQueryWrapper.eq(StrUtil.isNotBlank(param.getHospitalNumber()), BGenItem::getHospitalNumber, param.getHospitalNumber());
        }
        return headQueryWrapper;
    }

    private void busiKeyCheck(GenItemDto param, String fieldName, boolean isAdd, String errMsg) {

        QueryWrapper<BGenItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BGenItem::getModelCode, param.getModelCode());
        queryWrapper.eq(StrUtil.toUnderlineCase(fieldName), BeanUtil.getProperty(param, fieldName));
        queryWrapper.lambda().eq(BGenItem::getIsDeleted, 0);
        queryWrapper.lambda().eq(BGenItem::getIsSys, param.getIsSys());
        if (!isAdd) {
            queryWrapper.lambda().ne(BGenItem::getId, param.getId());
        }
        List<BGenItem> list = bGenItemMapper.selectList(queryWrapper);
        if (list != null && !list.isEmpty()) {
            throw new BizException(errMsg + "重复(" + Convert.toStr(BeanUtil.getProperty(param, fieldName)) + ")");
        }

    }

    private void syncSpecialCfg(GenItemSaveParam param, BGenItem paramGenItem, String itemNameDb) {
        LambdaQueryWrapper<BGenItem> queryWrapper = new LambdaQueryWrapper();
        if (!itemNameDb.equals(paramGenItem.getItemName())) {
            queryWrapper.eq(BGenItem::getModelCode, param.getModelCode());
            queryWrapper.eq(BGenItem::getIsDeleted, CommonConstant.NO_NUMBER);
            queryWrapper.eq(BGenItem::getItemName, itemNameDb);
            queryWrapper.eq(BGenItem::getIsSys, CommonConstant.NO_NUMBER);
            queryWrapper.isNotNull(BGenItem::getPatientIdent);
            BGenItem genItem = new BGenItem();
            genItem.setItemCode(paramGenItem.getItemCode());
            genItem.setItemName(paramGenItem.getItemName());
            genItem.setItemValueType(paramGenItem.getItemValueType());
            bGenItemMapper.update(genItem, queryWrapper);
        } else {
            queryWrapper.eq(BGenItem::getIsSys, CommonConstant.NO_NUMBER);
            queryWrapper.isNotNull(BGenItem::getPatientIdent);
            queryWrapper.eq(BGenItem::getItemName, paramGenItem.getItemName());
            queryWrapper.eq(BGenItem::getModelCode, param.getModelCode());
            BGenItem genItem = new BGenItem();
            genItem.setItemCode(paramGenItem.getItemCode());
            genItem.setOrderNo(paramGenItem.getOrderNo());
            genItem.setRemark(paramGenItem.getRemark());
            genItem.setItemValueType(paramGenItem.getItemValueType());
            bGenItemMapper.update(genItem, queryWrapper);
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long manualInsertByInOutModule(ManulGenItemDto manulGenItemDto) {
        //系统全局
        BGenItem genItemSystem = new BGenItem();
        //个性化配置
        BGenItem genItemPerson = new BGenItem();

        genItemSystem.setItemName(manulGenItemDto.getName());
        String code = "";

        code = Integer.toHexString((int) new Date().getTime());
        genItemSystem.setItemCode(code);
        //否
        genItemSystem.setIsVisible(0);
        genItemSystem.setItemValueType("text");
        genItemSystem.setIsDeleted(0);
        genItemSystem.setIsManual(1);
        genItemSystem.setRemark("出入量那边手动添加的");
        genItemSystem.setModelCode(manulGenItemDto.getModelCode());
        genItemSystem.setIsSys(1);
        genItemSystem.setOrderNo(999L);
        bGenItemMapper.insert(genItemSystem);

        //个性化配置记录保存
        genItemPerson.setIsManual(1);
        genItemPerson.setHospitalNumber(manulGenItemDto.getHospitalNumber());
        genItemPerson.setItemCode(code);
        genItemPerson.setItemName(manulGenItemDto.getName());
        genItemPerson.setItemValueType("text");
        genItemPerson.setRemark("出入量那边手动添加的");
        genItemPerson.setPatientIdent(manulGenItemDto.getPatientIdent());
        genItemPerson.setModelCode(manulGenItemDto.getModelCode());
        genItemPerson.setIsSys(0);
        genItemPerson.setIsVisible(1);
        genItemPerson.setIsDeleted(0);
        genItemPerson.setOrderNo(999L);
        bGenItemMapper.insert(genItemPerson);

        return genItemPerson.getId();
    }


    @Override
    public LinkedHashMap<String, BGenItem> queryAllByPid(String modeCode, String patientIdent) {
        LinkedHashMap<String, BGenItem> rst = new LinkedHashMap<>();
        GenItemQueryParam param = new GenItemQueryParam();
        param.setIsDeleted(CommonConstant.NO_NUMBER);
        param.setModelCode(modeCode);
        param.setIsSys(CommonConstant.NO_NUMBER);
        param.setPatientIdent(patientIdent);
        List<BGenItem> locaolList = queryAll(param);

        //当病人没有特别设置，则默认为全部
        param.setPatientIdent(null);
        param.setIsSys(CommonConstant.YES_NUMBER);
        param.setIsVisible(1);
        List<BGenItem> globalList = queryAll(param);

        if (CollectionUtil.isEmpty(locaolList)) {
            for (BGenItem item : globalList) {
                if (item == null || StrUtil.isBlank(item.getItemName())) {
                    continue;
                }
                if (item.getIsGeneral() == CommonConstant.NO_NUMBER) {
                    item.setIsVisible(CommonConstant.NO_NUMBER);
                }

                if (modeCode.equals("crl-in") || modeCode.equals("crl-out")) {
                    item.setIsVisible(item.getIsShowIo());
                }
                item.setIsSys(CommonConstant.YES_NUMBER);
                rst.put(item.getItemName(), item);
            }
            if (CollectionUtil.isNotEmpty(rst)) {
                LinkedHashMap<String, BGenItem> rest = new LinkedHashMap<>();
                List<BGenItem> values = rst.values().stream().sorted(Comparator.comparing(BGenItem::getIsGeneral).reversed())
                        .sorted(Comparator.comparing(BGenItem::getOrderNo).reversed()).collect(Collectors.toList());
                for (BGenItem item : values) {
                    rest.put(item.getItemName(), item);
                }
                return rest;
            }
            return rst;
        } else {
            Map<String, BGenItem> lMap = locaolList.stream()
                    .filter(o -> o != null && StrUtil.isNotBlank(o.getItemName()))
                    .map(o -> {
                        rst.put(o.getItemName(), o);
                        return o;
                    })
                    .collect(Collectors.toMap(BGenItem::getItemName, o -> o, (o1, o2) -> o1));
            for (BGenItem item : globalList) {
                if (item == null || StrUtil.isBlank(item.getItemName())) {
                    continue;
                }
                if (!lMap.containsKey(item.getItemName())) {
                    if (item.getIsGeneral() == CommonConstant.NO_NUMBER) {
                        item.setIsVisible(CommonConstant.NO_NUMBER);
                    }
                    if (modeCode.equals("crl-in") || modeCode.equals("crl-out")) {
                        item.setIsVisible(item.getIsShowIo());
                    }
                    rst.put(item.getItemName(), item);
                } else {
                    BGenItem yxGenItem = BeanUtil.copyProperties(lMap.get(item.getItemName()), BGenItem.class);
                    rst.put(item.getItemName(), yxGenItem);
                }
            }
        }
        if (CollectionUtil.isNotEmpty(rst)) {
            LinkedHashMap<String, BGenItem> rest = new LinkedHashMap<>();
            List<BGenItem> values = rst.values().stream().sorted(Comparator.comparing(BGenItem::getIsGeneral).reversed())
                    .sorted(Comparator.comparing(BGenItem::getOrderNo).reversed()).collect(Collectors.toList());
            for (BGenItem item : values) {
                rest.put(item.getItemName(), item);
            }
            return rest;
        }
        return rst;
    }

    /**
     * 新增或保存存病人配置
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertOrUpdate(List<GenItemSaveParam> param, boolean isGloble) {
        if (CollectionUtil.isEmpty(param)) {
            return;
        }
        for (GenItemSaveParam item : param) {
            if (item == null) {
                continue;
            }
            if (isGloble) {
                item.setIsSys(CommonConstant.YES_NUMBER);
            } else {
                item.setIsSys(CommonConstant.NO_NUMBER);
            }
            insertOrUpdate(item);
        }
    }


    @Override
    public void importExcel(MultipartFile file) {
        //todo 框架 待实现
        try {
            Map<String, Object> rst = EasyExcelUtil.importExcel(file, GenItemExcelDto.class, SHEET_1, 2,
                    (row -> {
                        return null;
                    })
            );
        } catch (IOException e) {
            e.printStackTrace();
            throw new BizException("导入sheet1时出现异常");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void quickcreate(GenItemQuickSaveParam param) {
        List<DefDictItemResultVO> mods = defDictMapper.getDictDetailsByParentKey("gen_model");
        if (CollectionUtil.isEmpty(mods)) {
            throw new BizException("该模块不存在");
        }
        Map<String, List<DefDictItemResultVO>> map = mods.stream().collect(Collectors.groupingBy(DefDictItemResultVO::getKey));
        if (CollectionUtil.isEmpty(map)) {
            throw new BizException("该模块不存在");
        }
        if (!map.containsKey(param.getModelCode())) {
            throw new BizException("该模块不存在");
        }
        GenItemDto dto = ConvertUtil.convert(param, GenItemDto.class);
        dto.setIsSys(CommonConstant.YES_NUMBER);
        busiKeyCheck(dto, ITEM_NAME, true, "项目名称");
        BGenItem po = ConvertUtil.convert(param, GenItemDto.class);
        po.setIsSys(CommonConstant.YES_NUMBER);
        po.setItemCode(po.getModelCode() + "-" + UUID.randomUUID().toString(true));
        po.setIsVisible(CommonConstant.YES_NUMBER);
        po.setItemValueType("text");
        if (StrUtil.isNotEmpty(po.getPatientIdent())) {
            po.setIsGeneral(CommonConstant.NO_NUMBER);
            BGenItem pol = ConvertUtil.convert(po, BGenItem.class);
            pol.setIsSys(CommonConstant.NO_NUMBER);
            pol.setIsVisible(CommonConstant.YES_NUMBER);
            po.setPatientIdent(null);
            bGenItemMapper.insert(po);
            bGenItemMapper.insert(pol);
        } else {
            po.setIsGeneral(CommonConstant.YES_NUMBER);
            bGenItemMapper.insert(po);
        }
        List<BCareRecordsConfig> hlcz = bCareRecordsConfigMapper.selectMainByCode(param.getDictValue());
        if (CollectionUtil.isNotEmpty(hlcz)) {
            List<BCareRecordsConfig> dpo = bCareRecordsConfigMapper.selectDetailByPidAndCode(hlcz.get(0).getId(), po.getItemCode());
            List<BCareRecordsConfig> dpoweight = bCareRecordsConfigMapper.selectMaxWeightDetailByPid(hlcz.get(0).getId());
            BCareRecordsConfig addPo = ConvertUtil.convert(hlcz.get(0), BCareRecordsConfig.class);
            addPo.setId(null);
            if (CollectionUtil.isNotEmpty(dpo)) {
                addPo.setId(dpo.get(0).getId());
            }
            addPo.setItemCode(po.getItemCode());
            addPo.setTitle(po.getItemName());
            addPo.setPid(hlcz.get(0).getId());
            if (CollectionUtil.isNotEmpty(dpoweight) && null != dpoweight.get(0).getWeights()) {
                addPo.setWeights(dpoweight.get(0).getWeights() + 1);
            } else {
                addPo.setWeights(1);
            }
            if (null == addPo.getId()) {
                bCareRecordsConfigMapper.insert(addPo);
            } else {
                bCareRecordsConfigMapper.updateById(addPo);
            }

        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void quickdelete(GenItemQuickDeleteParam param) {
        List<DefDictItemResultVO> mods = defDictMapper.getDictDetailsByParentKey("gen_model");
        if (CollectionUtil.isEmpty(mods)) {
            throw new BizException("该模块不存在");
        }
        Map<String, List<DefDictItemResultVO>> map = mods.stream().collect(Collectors.groupingBy(DefDictItemResultVO::getKey));
        if (CollectionUtil.isEmpty(map)) {
            throw new BizException("该模块不存在");
        }
        if (!map.containsKey(param.getModelCode())) {
            throw new BizException("该模块不存在");
        }
        List<BGenItem> items = bGenItemMapper.selectSysListByCodeAndModel(param.getItemCode(), param.getModelCode());
        if (CollectionUtil.isNotEmpty(items)) {
            bGenItemMapper.deleteByCodeAndModel(param.getItemCode(), param.getModelCode());
            bGenItemDetailService.deleteByParentId(items.get(0).getId());
        }
        List<BCareRecordsConfig> hlcz = bCareRecordsConfigMapper.selectMainByCode(param.getDictValue());
        if (CollectionUtil.isNotEmpty(hlcz)) {
            List<BCareRecordsConfig> dpo = bCareRecordsConfigMapper.selectDetailByPidAndCode(hlcz.get(0).getId(), param.getItemCode());
            if (CollectionUtil.isNotEmpty(dpo)) {
                bCareRecordsConfigMapper.deleteById(dpo.get(0));
            }


        }
    }

    @Override
    public List<BGenItem> queryItemByPidAndCode(String modeCode,String itemCodes,String patientIdent) {
        return bGenItemMapper.queryItemByPidAndCode(modeCode,itemCodes,patientIdent);
    }

    @Override
    public List<BGenItem> queryByNameNoDel(String modelCode, String itemName) {
        if (StrUtil.isBlank(modelCode) || StrUtil.isBlank(itemName)) {
            return null;
        }
        LambdaQueryWrapper<BGenItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BGenItem::getModelCode, modelCode);
        wrapper.eq(BGenItem::getItemName, itemName);
        wrapper.eq(BGenItem::getIsDeleted, CommonConstant.NO_NUMBER);
        wrapper.orderByDesc(BGenItem::getUpdatedTime);
        return bGenItemMapper.selectList(wrapper);
    }

    @Override
    public List<BGenItem> queryItemByPid(String modeCode, String patientIdent) {
        return bGenItemMapper.queryItemByPidAndCode(modeCode,null,patientIdent);
    }
}


